package com.zz.service;

import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.zz.entity.Entry;
import com.zz.entity.Po;
import com.zz.entity.Poreturns;
import com.zz.entity.Product;
import com.zz.entity.Supplier;
import com.zz.entity.Ware;
import com.zz.repository.EntryRepository;
import com.zz.repository.PoRepository;
import com.zz.repository.PoreturnsRepository;
import com.zz.repository.ProductRepository;
import com.zz.repository.SupplierRepository;
import com.zz.repository.WareRepository;

@Service
public class EntryService {
	@Resource
	WareRepository wareRepository;
	@Resource
	SupplierRepository supplierRepository;
	@Resource
	ProductRepository productRepository;
	@Resource
	EntryRepository entryRepository;
	@Resource
	PoRepository poRepository;
	@Resource
	PoreturnsRepository poreturnsRepository;
	
	//查询所有仓库信息
	public List<Ware> findAllWare(){
		return wareRepository.findAll();
	}
	//查询所有供应商信息
	public List<Supplier> findAllSupplier(){
		return supplierRepository.findAll();
	}
	//查询是否存在产品信息
	public Product findByNameAndTypeAndWareIdAndSupplierId(String name,String type,String wareid,String suppid) {
		return productRepository.findByNameAndTypeAndWareIdAndSupplierId(name, type, wareid, suppid);
	}
	//保存--更新产品信息
	public Product saveproduct(Product p) {
		return productRepository.save(p);
	}
	//根据名字查询产品信息
	public List<Object[]> selectproductRKlistByname(String name){
		return productRepository.selectproductRKlistByname(name);
	}
	//查询产品Byid
	public Optional<Product> findproductById(String id) {
		return productRepository.findById(id);
	}
	//保存入库单信息
	public Entry saveEntry(Entry entrylist) {
		return entryRepository.save(entrylist);
	}
	//显示所有入库单并进行分页
	public Page<Object[]> showAllEntrylist(String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return entryRepository.showAllEntrylist(pageable);
	}
	//最近一周
	public Page<Object[]> showEntrylistBy7day(String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return entryRepository.showEntrylistBy7day(pageable);
	}
	//最近一个月
	public Page<Object[]> showEntrylistBy30day(String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return entryRepository.showEntrylistBy30day(pageable);
	}
	public Page<Object[]> findprofuctIdByPWS30day(String product_id,String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return entryRepository.findprofuctIdByPWS30day(product_id, pageable);
	}
	//一段时间查询
	public Page<Object[]> showEntrylistBytime(String starttime,String lasttime,String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return entryRepository.showEntrylistBytime(starttime, lasttime, pageable);
	}
	//查询所有采购单
	public Page<Object[]> showAllPolist(String page,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit));
		return poRepository.showAllPolist(pageable);
	}
	//根据产品名字和仓库名查询供应商名字
	public List<Object[]> selectproductlistByPrnameandWarename(String productname,String warename){
		return productRepository.selectproductlistByPrnameandWarename(productname, warename);
	}
	//找到产品id
	public List<Object[]> findprofuctIdByPWS(String productname,String warename,String suppliername) {
		return productRepository.findprofuctIdByPWS(productname, warename, suppliername);
	}
	//存储采购单
	public Po savePolist(Po po) {
		return poRepository.save(po);
	}
	//7天查询采购单
	public Page<Object[]> showPolistBy7Day(String page,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit));
		return poRepository.showPolistBy7Day(pageable);
	}
	public Page<Object[]> showPolistBy30Day(String page,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit));
		return poRepository.showPolistBy30Day(pageable);
	}
	public Page<Object[]> showPolistBytime(String starttime,String lasttime,String page,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit));
		return poRepository.showPolistBytime(starttime, lasttime, pageable);
	}
	//查询采购单id
	public Optional<Po> findPoById(String id) {
		return poRepository.findById(id);
	}
	public List findPolistById(String id) {
		return poRepository.showPolistByid(id);
	}
	//退货信息存储
	public Poreturns savePoreturns(Poreturns poreturns) {
		return poreturnsRepository.save(poreturns);
	}
	//退货信息查询
	public List findPoreturnsbyPoid(String id) {
		return poreturnsRepository.findPoreturnsbyPoid(id);
	}
}
